﻿using System;
using System.Data;
using System.Windows.Forms;
using ChinaDynastyTours.Controls;
using ChinaDynastyTours.Helpers;
using IToursDataService.Models;
using IToursDataService;

namespace ChinaDynastyTours.Views
{
    public partial class RestaurantOrderForm : DevExpress.XtraEditors.XtraForm
    {
        //输入参数
        public string _TourGroupID { get; set; }//团ID
        public string _LocalPlanScheduleDate { get; set; }//团状态
        public string _LocalPlanScheduleID { get; set; }//行程计划ID
        public string _TourGroupScheduleID { get; set; }//大程计划ID
        public string _LoginProviderID { get; set; }

        //输出三处
        public string _returnRestaurant { get; set; }//返回选择的餐厅内容

        public DataTable _dtOrderData;

        public RestaurantOrderForm()
        {
            InitializeComponent();
        }
       
        private void RestaurantOrderForm_Load(object sender, EventArgs e)
        {
            //初始化城市下拉列表
            tbCity.Properties.DataSource = WcfClient.UseService((IToursDataService.IBaseInfo b) => (b.GetCityData())); //BaseInfoCtrl.GetCityData();

            string cityName = WcfClient.UseService((IToursDataService.IBaseInfo b) => (b.GetProviderCityNameById(_LoginProviderID))); //BaseInfoCtrl.GetProviderCityNameById(_LoginProviderID);
            tbCity.EditValue = cityName;

            tbCity_EditValueChanged(null, null);
            LoadOrderGridPageData();
        }

        private void LoadOrderGridPageData()
        {
            orderGridControl.DataSource = _dtOrderData = WcfClient.UseService<IRestaurantOrderCtrl, DataTable>(r => r.GetRestaurantOrderData(_TourGroupID, _LocalPlanScheduleID));//RestaurantOrderCtrl.GetRestaurantOrderData(_TourGroupID, _LocalPlanScheduleID);
            _dtOrderData.ColumnChanged += new DataColumnChangeEventHandler(OrderData_ColumnChanged);
        }

        #region 修改预订信息后的自动保存

        void OrderData_ColumnChanged(object sender, DataColumnChangeEventArgs e)
        {
            int providerId = int.Parse(e.Row["ProviderID"].ToString());
            string providerName = e.Row["ProviderName"].ToString();
            string serverName = e.Row["ServiceName"].ToString();

            RestaurantOrderEntity restaurantOrder = new RestaurantOrderEntity();

            restaurantOrder.RestaurantOrderID = int.Parse(e.Row["RestaurantOrderID"].ToString());/*重点字段*/

            restaurantOrder.ServiceDate = DateTime.Parse(e.Row["ServiceDate"].ToString()).ToString("yyyy-MM-dd");
            restaurantOrder.ProviderID = providerId;
            restaurantOrder.ProviderName = providerName;
            restaurantOrder.ServiceName = serverName;

            WcfClient.UseService<IRestaurantOrderCtrl, int>(r => r.UpdateRestaurantOrderData(restaurantOrder));//RestaurantOrderCtrl.UpdateRestaurantOrderData(restaurantOrder);
            //刷新数据
            LoadOrderGridPageData();
        }
        #endregion
        
        private void ProviderGridView_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Clicks == 2)
            {
                btnAdd.PerformClick();
            }
            //刷新数据
            LoadOrderGridPageData();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            
            //decimal adultUnitPrice = 0;
            //decimal accompanyUnitPrice = 0;
            string serverName = "";
            int providerId = 0;
            string providerName = "";
            string typeBelong = ProviderTypeEnum.餐厅.ToString();

            serverName = "午餐";//默认设置 //restaurantGridView.GetRowCellValue(e.RowHandle, "ServiceName").ToString();
            providerId = int.Parse(restaurantGridView.GetRowCellValue(restaurantGridView.FocusedRowHandle, "ProviderID").ToString());
            providerName = restaurantGridView.GetRowCellValue(restaurantGridView.FocusedRowHandle, "ProviderName").ToString();

            //创建娱乐预定信息内容
            RestaurantOrderEntity restaurantOrder = new RestaurantOrderEntity();
            restaurantOrder.TourgroupID = _TourGroupID;
            restaurantOrder.TourgroupName = WcfClient.UseService<ITeamGroupCtrl, string>(t => t.GetTeamGroupNameById(_TourGroupID)); //TeamGroupCtrl.GetTeamGroupNameById(_TourGroupID);
            restaurantOrder.ProviderID = providerId;
            restaurantOrder.ProviderName = providerName;
            restaurantOrder.ServiceName = serverName;
            restaurantOrder.LocalPlanScheduleID = int.Parse(_LocalPlanScheduleID);
            restaurantOrder.ServiceDate = DateTime.Parse(_LocalPlanScheduleDate).ToString("yyyy-MM-dd");
            restaurantOrder.Remark = "";

            WcfClient.UseService<IRestaurantOrderCtrl, int>(r => r.CreateRestaurantOrderData(restaurantOrder));//RestaurantOrderCtrl.CreateRestaurantOrderData(restaurantOrder);

            //更新团审核成本
            //TeamGroupCtrl.UpdateTourGroupBudgeCost(_TourGroupID);
            //刷新数据
            LoadOrderGridPageData();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (orderGridView.FocusedRowHandle < 0) return;

            WcfClient.UseService<IRestaurantOrderCtrl, int>(r => r.DeleteRestaurantOrder(orderGridView.GetRowCellValue(orderGridView.FocusedRowHandle, "RestaurantOrderID").ToString()));//RestaurantOrderCtrl.DeleteRestaurantOrder(orderGridView.GetRowCellValue(orderGridView.FocusedRowHandle, "RestaurantOrderID").ToString());

            //更新团审核成本
            WcfClient.UseService<ITeamGroupCtrl, int>(t => t.UpdateTourGroupBudgeCost(_TourGroupID)); //TeamGroupCtrl.UpdateTourGroupBudgeCost(_TourGroupID);
            //刷新数据
            LoadOrderGridPageData();
        }

        #region 关闭窗口保存数据
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void EntertainmenRestaurantOrderForm_FormClosing(object sender, FormClosingEventArgs e)
        {

            //激发团数据更新,转移一下焦点即可
            btnExit.Focus();
            if (_dtOrderData == null)
                return;
            foreach (DataRow dr in _dtOrderData.Rows)
            {
                _returnRestaurant += string.Format(" {0} {1}\r\n", dr["ServiceName"].ToString().Substring(0, 1), dr["ProviderName"]);
            }
        } 
        #endregion

        private void restaurantGridView_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Clicks == 2)
            {
                btnAdd.PerformClick();
            }
        }

        private void tbCity_EditValueChanged(object sender, EventArgs e)
        {
            DataTable tmpDt = WcfClient.UseService((IToursDataService.IBaseInfo b) => (b.GetProviderData(tbCity.EditValue.ToString(), ProviderTypeEnum.餐厅))); //BaseInfoCtrl.GetProviderData(tbCity.EditValue.ToString(), ProviderTypeEnum.餐厅);
            btnAdd.Enabled = tmpDt.Rows.Count > 0 ? true : false;
            btnDelete.Enabled = tmpDt.Rows.Count > 0 ? true : false;

            restaurantGridControl.DataSource = tmpDt;
        }

    }
}
